package mysql

import (
	"class-project/config"
	"class-project/global"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var err error

func InitMysql() {
	app := config.AppConf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", app.User, app.Password, app.Host, app.Port, app.Database)
	global.Db, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("mysql数据库连接失败")
		return
	}
	fmt.Println("mysql数据库连接成功")
	global.Db.AutoMigrate(new(User), new(Class), new(Cart))
}
